﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicJournal.Account.AuthorAccount
{
    public partial class NewPublication : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (PublicationIdLabel.Text.Length == 0)
            {
                PublicationIdLabel.Text = Guid.NewGuid().ToString();
                //SqlDataSource3.SelectParameters[0].DefaultValue = PublicationIdLabel.Text;
            }
        }

        protected void AddNewAuthor(object sender, EventArgs e)
        {
            SqlDataSource3.InsertParameters[0].DefaultValue = PublicationIdLabel.Text;
            SqlDataSource3.Insert();
        }

        protected void AddNewPublication(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            string folder = Server.MapPath("..") + "\\files\\publications\\" + PublicationIdLabel.Text + "\\";
            System.IO.Directory.CreateDirectory(folder);

            DB.Item.ItemPublication item = new DB.Item.ItemPublication()
                                               {

                                                   //AuthorId = Membership.GetUser().ProviderUserKey.ToString(),
                                                   TitleRussian = TitleRussianTextBox.Text,
                                                   TitleEnglish = TitleEnglishTextBox.Text,
                                                   ScientificDirection = ScientificDropDownList.SelectedValue,
                                                   Type = TypeDropDownList.SelectedValue,
                                                   UDCIndex = UDCDropDownList.SelectedItem.Text,
                Speciality = SpecialityTextBox.Text,
                SummaryRussian = SummaryRussianTextBox.Text,
                SummaryEnglish = SummaryEnglishTextBox.Text,
                KeywordsRussian = KeywordsRussianTextBox.Text,
                KeywordsEnglish = KeywordsEnglishTextBox.Text,
                FileArticle = UploadFileOnServer(FileArticleUpload, folder),
                FileReview1 = UploadFileOnServer(FileReview1Upload, folder),
                FileReview2 = UploadFileOnServer(FileReview2Upload, folder),
                FileLetter = UploadFileOnServer(FileLetterUpload, folder),
                FileExpert = UploadFileOnServer(FileExpertUpload, folder),
                Date = now,
                State = 0,
                Id = PublicationIdLabel.Text
            };
            if (new DB.DbPublications().CreateNewPublication(item))
            {
                TitleRussianTextBox.Text = null;
                TitleEnglishTextBox.Text = null;
                ScientificDropDownList.SelectedIndex = 0;
                TypeDropDownList.SelectedIndex = 0;
                UDCDropDownList.SelectedIndex = 0;
                SpecialityTextBox.Text = null;
                SummaryRussianTextBox.Text = null;
                SummaryEnglishTextBox.Text = null;
                KeywordsRussianTextBox.Text = null;
                KeywordsEnglishTextBox.Text = null;
            }
            else
            {
                ErrorLabel.Text = "Неверные входные данные";
            }
        }

        private string UploadFileOnServer(FileUpload uploader, string folder)
        {

            string FileName = folder + System.IO.Path.GetFileName(uploader.FileName);
            if ((uploader.PostedFile != null) && (uploader.PostedFile.ContentLength > 0))
            {
                uploader.PostedFile.SaveAs(FileName);
            }
            return FileName.Remove(0, FileName.LastIndexOf("\\") + 1);
        }
    }
}